package mx.ipn.presentacion.nomina;
import java.sql.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class NominaDB extends dao
{
	String id;
	String tabla;
	String clase;
	String query;
	Connection conexion;
	PreparedStatement updateSales; 
	ResultSet rs;
	public NominaDB(String id,String tabla,String clase)
	{
	   try{
	   	  this.id=id;
	   	  this.tabla=tabla;
	   	  this.clase=clase;
          conectarBaseDatos();
          conexion = getConexion();
       }
       catch(Exception e){
          System.out.println(e.getMessage());
       }	
	}
	public void setQuery(String query)
	{
		this.query=query;
	}
	public ResultSet getDatos()
	{
		try{
		updateSales = conexion.prepareStatement(query);
	   	rs =updateSales.executeQuery();
	    }
		catch(Exception e){
          System.out.println(e.getMessage() + "error en db");
        }
		return rs;
	}
	public static String escaparSQL(String s)
	{
		String sql = s;
		if(sql!=null){
                sql = sql.replaceAll("'", "''");
                sql = sql.replaceAll("\"", "'\"");
                sql = sql.replaceAll("%", "'%");
                sql = sql.replaceAll("_", "'_");
        }
                return sql;
	}
	public boolean guardar(String []datos)
	{
	   boolean flag=false;
	   try
	   {
	   	String busca="select * from " + this.tabla + " where " + this.id + " = " + datos[0];
	   	String comando="";
	   	String campos="";
	   	String signos="";
	   	String actual="";
	   	int j=0;
	   	Statement cons = conexion.createStatement();
	   	ResultSet rs = cons.executeQuery(busca);
	   	Class t = Class.forName(this.clase);
	   	Field []fields= t.getDeclaredFields(); 
	   	for(j=0;j<(fields.length-1);j++)
	   	{
	   	   campos = campos + fields[j].getName() +",";
	   	}
	   	campos = campos + fields[j].getName();
	   	for(j=0;j<(datos.length-1);j++)
	   	{
	   	   signos = signos + "?,";
	   	}
	   	signos = signos + "?";
	   	if(!rs.first()){
           comando= "insert into "+this.tabla+"("+ campos +") values ("+signos+")";
		   PreparedStatement updateSales = conexion.prepareStatement(comando);
		   for(j=0;j<fields.length;j++)
	   	   {
	   	      if(fields[j].getType().getName().compareTo("java.lang.String")==0)
	   	         updateSales.setString(j+1,datos[j]); 
	   	      if(fields[j].getType().getName().compareTo("double")==0)
	   	         updateSales.setDouble(j+1,Double.parseDouble(datos[j])); 
	   	      if(fields[j].getType().getName().compareTo("int")==0)
	   	         updateSales.setInt(j+1,Integer.parseInt(datos[j]));
	   	   }
	       updateSales.executeUpdate();	
	       updateSales.close();
	   }
	   else
	   {
	      for(j=0;j<(fields.length-1);j++)
	      {
	   	     if(fields[j].getType().getName().compareTo("java.lang.String")==0){
	   	        actual = actual + fields[j].getName() +" = '" + datos[j] +"',";
	   	     }
	   	     else{
	   	        actual = actual + fields[j].getName() +" = " + datos[j] +",";
	   	     }
	   	  }
	   	  if(fields[j].getType().getName().compareTo("java.lang.String")==0){
	   	     actual = actual + fields[j].getName() +"= '" + datos[j] + "'";
          }
          else
          {
             actual = actual + fields[j].getName() +"=" + datos[j];
          }
	   	  comando = "update " + this.tabla +" set " + actual + " where " + this.id + " = " + datos[0];
	      cons.executeUpdate(comando);
	   }
	   rs.close();
	   cons.close();
	   flag=true;
       }
       catch(Exception e){
          System.out.println(e.getMessage()+ "error en insertar factura");
          flag=false;
       }
       return flag;
	}
	public boolean eliminar(String id)
	{
	   boolean res=false;
	   try
	   {
		   Statement cons = conexion.createStatement();
           String comando ="delete from " + this.tabla +" where " + this.id + " = " + id; 
		   cons.executeUpdate(comando);
	       cons.close();
	       res=true;
       }
       catch(Exception e){
          System.out.println(e.getMessage()+ "error en getIdfactura");
       }
       return res;
	}
}